/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.api;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.jeesite.modules.storage.common.ServerResponse;
import com.jeesite.modules.storage.util.WebUtil;
import gui.ava.html.image.generator.HtmlImageGenerator;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;


/**
 * 测试Controller
 */
@RestController(value = "apitest")
@RequestMapping(value = "/api/test")
public class TestController extends APIController {

    @RequestMapping(method = RequestMethod.GET, value = "html2image")
    @ResponseBody
    public ServerResponse<Map> html2image() {
        HtmlImageGenerator imageGenerator = new HtmlImageGenerator();
        String htmlStr = WebUtil.readFileToString(WebUtil.getHtmlPath("1.html"));
        imageGenerator.loadHtml(htmlStr);
        imageGenerator.getBufferedImage();
        imageGenerator.saveAsImage("d:/temp/html2image.png");

        Map map = new HashMap();
        return ServerResponse.createBySuccess(200, map);
    }

    @RequestMapping(method = RequestMethod.GET, value = "thumbnailator")
    @ResponseBody
    public ServerResponse<Map> thumbnailator() {

        try {
            Thumbnails.of("D:\\Temp\\1216721574190886914.png").size(200, 300).keepAspectRatio(false).toFile("D:\\Temp\\1216721574190886914_dest.png");
        } catch (Exception e1) {
        }


        Map map = new HashMap();
        return ServerResponse.createBySuccess(200, map);
    }

    private void writeImage() {
        try {
        int BLACK = 0x000000;
        int WHITE = 0xFFFFFF;
        String FORMAT_NAME = "png";

        Map<EncodeHintType, Object> hints = new HashMap<>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.MARGIN, "0");

        //根据高度和宽度生成像素矩阵
        MultiFormatWriter writer = new MultiFormatWriter();
        BitMatrix bitMatrix = writer.encode("http://www.baidu.com/", BarcodeFormat.QR_CODE, 104, 104, hints);

        BufferedImage image = new BufferedImage(bitMatrix.getWidth(), bitMatrix.getHeight(), BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < bitMatrix.getWidth(); x++) {
            for (int y = 0; y < bitMatrix.getHeight(); y++) {
                //填充黑白两色
                image.setRGB(x, y, bitMatrix.get(x, y) ? BLACK : WHITE);
            }
        }

        String fileName = new Random().nextInt(99999999) + ".png";//生成随机文件名
        File imgFile = new File("D:\\Temp\\" + fileName);
        ImageIO.write(image, FORMAT_NAME, imgFile);
    } catch(Exception e)
    {
        e.printStackTrace();
    }
}

private  void readImage(){
    String filePath = "D:\\Temp\\84767963.jpg";
    BufferedImage image;
    try {
        image = ImageIO.read(new File(filePath));
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        Binarizer binarizer = new HybridBinarizer(source);
        BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
        Map<DecodeHintType, Object> hints = new HashMap<DecodeHintType, Object>();
        hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
        Result result = new MultiFormatReader().decode(binaryBitmap, hints);// 对图像进行解码
        String text = result.getText();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
    @RequestMapping(method = RequestMethod.GET,value = "zxing")
    @ResponseBody
    public ServerResponse<Map> zxing(){
            writeImage();
            readImage();

        Map map = new HashMap();
        return ServerResponse.createBySuccess(200,map);
    }

    @RequestMapping(method = RequestMethod.GET,value = "auto")
    @ResponseBody
    public ServerResponse<String> auto(){
        return ServerResponse.createBySuccess(200,"222222222222");
    }

    @RequestMapping(method = RequestMethod.GET,value = "gt")
    @ResponseBody
    public ServerResponse<String> gt(String clientId){
        return ServerResponse.createBySuccess(200,null);
    }
}
